<div class="divColorbox">
    <div class="divColorbox_Header">
        <span><i class="fa fa-star"></i><span>礼物发放</span></span>
        <a class="Refresh"></a><a class="Close"></a>
    </div>
    <div class="divColorbox_Body row">
        <div>
            <input name="User_ID" type="hidden"/>
            <div class="row">
                <div class="col-sm-4">
                    <div class="input-group">
                        <span class="input-group-addon">商品分类</span>
                        <select class="form-control" name="Item_Type" id="Item_Type">
                            <option value="" selected>--</option>
                            <option value="2">座驾</option>
                            <option value="1">VIP</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-4">
                    <div class="input-group">
                        <span class="input-group-addon">商品名称</span>
                        <select class="form-control" name="Item_Name" id="Item_Name">
                        </select>
                    </div>
                </div>
            </div>
            <div class="space-6"></div>
            <div class="row">
                <div class="col-sm-4">
                    <div class="input-group">
                        <span class="input-group-addon">有效期/天</span>
                        <input id="Expire" type="text" class="form-control" />
                    </div>
                </div>
            </div>
            <div class="space-6"></div>
            <div class="row">
                <div class="col-sm-4">
                    <div class="input-group">
                        <span class="input-group-addon">数量</span>
                        <input id="Num" type="text" class="form-control" />
                    </div>
                </div>
            </div>
            <div class="space-6"></div>
            <div class="row">
                <div class="col-sm-4">
                    <div class="input-group">
                        <span class="input-group-addon">用户ID</span>
                        <input id="User_ID" type="text" class="form-control" />
                        <span class="input-group-addon" id="AddGrant_NickName" style="color: #ff0000;">输入正确的用户,会出现昵称</span>
                    </div>
                </div>
            </div>
            <div class="space-6"></div>
            <div class="row">
                <div class="col-sm-4">
                    <div class="input-group">
                        <span class="input-group-addon">发放理由</span>
                        <input id="Remark" type="text" class="form-control" />

                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="divColorbox_Footer">
        <button type="button" id="btnEdit_Agree" class="btn btn-primary">
            <i class="fa fa-check"></i> 确定
        </button>
        <button type="button" id="btnUndo"></button>
    </div>
</div>
<script>
    $(function () {
        if (_PageData.flag != 0) {
            $.KOC.Tips.Open("Error", _PageData.flag, {
                Timeout: 1000,
                onClose: function () {
                    parent.$.colorbox.close();
                }
            });
            return;
        }
        //////////////////////////////////////////////////////////
        //验证用户ID
        //////////////////////////////////////////////////////////
        $("#User_ID").keyup(function(){
            var params = {};
            params.User_ID = $.trim($('#User_ID').val());//公会Id
            if(params.familyId !=''){
                $.KOC.Loading.Open();
                $.ajax({
                    url: '/ajax/base.user-id-info',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        User_ID:params.User_ID,
                    },
                    success: function (data) {
                        $.KOC.Loading.Close();
                        if (data.flag !== 0) {
                            $.KOC.Tips.Open("Error", data.msg , {Timeout: 2000});
                            $('#AddGrant_NickName').html('');
                            return;
                        }
                        $.KOC.Tips.Open("Success", '验证成功' , {Timeout:1000});
                        $('#AddGrant_NickName').empty().html(data.NickName);
                    }
                });
            }else{
                $("#User_ID").val('');
                $('#AddGrant_NickName').html('');
                return true;
            }
        })
        //////////////////////////////////////////////////////////
        //物品发放通过
        //////////////////////////////////////////////////////////
        $('#btnEdit_Agree').on('click', function () {
            var params = {};
            params.Item_Type = $('#Item_Type').val();
            params.Item_ID = $('#Item_Name').val();
            params.Item_Name = $('#Item_Name  option:selected').text();
            params.Expire = $.trim($('#Expire').val());
            params.Num = $.trim($('#Num').val());
            params.User_ID = $.trim($('#User_ID').val());
            params.User_NickName = $.trim($('#AddGrant_NickName').text());
            params.Remark = $.trim($('#Remark').val());
            if (!params.Item_Type) {
                $.KOC.Tips.Open('Error', '请选择商品分类', {Timeout: 2000});
                return false;
            }
            if (!params.Item_Name) {
                $.KOC.Tips.Open('Error', '请选择商品', {Timeout: 2000});
                return false;
            }
            if (!params.Expire) {
                $.KOC.Tips.Open('Error', '有效期不能不为空', {Timeout: 2000});
                return false;
            }
            if (!params.Num) {
                $.KOC.Tips.Open('Error', '数量不能为空', {Timeout: 2000});
                return false;
            }
            if (!params.User_ID) {
                $.KOC.Tips.Open('Error', '用户ID不能为空', {Timeout: 2000});
                return false;
            }
            if (!params.Remark) {
                $.KOC.Tips.Open('Error', '发放理由不能为空', {Timeout: 2000});
                return false;
            }
            if (!params.User_NickName) {
                $.KOC.Tips.Open('Error', '你输入的用户ID不正确', {Timeout: 2000});
                return false;
            }
            $.KOC.Loading.Open();
            $.ajax({
                url: '/ajax/user.user-info.grantitem',
                type: 'post',
                dataType: 'json',
                data: {
                    User_ID: params.User_ID,
                    User_NickName: params.User_NickName,
                    Item_Type: params.Item_Type,
                    Item_ID: params.Item_ID,
                    Item_Name: params.Item_Name,
                    Num: params.Num,
                    Item_Expire: params.Expire,
                    Remark: params.Remark,
                },
                success: function (data) {
                    $.KOC.Loading.Close();
                    if (data.flag !== 0) {
                        $.KOC.Tips.Open("Error", data.msg + "(" + data.flag + ")", {Timeout: 2000});
                        return;
                    }
                    $.KOC.Tips.Open('Success', '物品发放成功', {
                        Timeout: 1000,
                        onClose: function () {
                            parent.$.colorbox.close();
                            parent.window.location.href= "/user-manage/info-manage/grantlist";
                        }
                    });
                }
            });
        });
        $('#Item_Type').change(function () {
            var type=$(this).val();
            if(type==1){
                VIPShow();
            }else{
                MountsShow();
            }
        });
        //////////////////////////////////////////////////////////
        //VIP
        //////////////////////////////////////////////////////////
        function VIPShow(){
            var vip = '';
            for (var n = 0; n < _PageData.VIP.length; n++) {
                if (n==0) {
                    vip += '<option value="' + _PageData.VIP[n].id + '"  selected>VIP</option>';
                } else {
                    vip += '<option value="' + _PageData.VIP[n].id + '" >VIP</option>';
                }
            }
            $('#Item_Name').empty().append(vip);
        }
        //////////////////////////////////////////////////////////
        //座驾
        //////////////////////////////////////////////////////////
        function MountsShow(){
            var Mount = '';
            for (var n = 0; n < _PageData.Mounts.length; n++) {
                if(_PageData.Mounts[n].id > 0){
                    if (n==0) {
                        Mount += '<option value="' + _PageData.Mounts[n].id + '"  selected>' + _PageData.Mounts[n].Name + '</option>';
                    } else {
                        Mount += '<option value="' + _PageData.Mounts[n].id + '" >' + _PageData.Mounts[n].Name + '</option>';
                    }
                }

            }
            $('#Item_Name').empty().append(Mount);
        }

    });
</script>